<?php
 
Class indexController extends appController
{

	public function main()
	{
		
		$this->display('main.html');
	}
	
	public function cutimg()
	{
		$img=$_GET['i'];
		$file_path=ROOT_PATH.$img;
		
		if(is_file($file_path))
		{
			$arr=getimagesize($file_path);
			$img_info['src']=$img;
			$img_info['w']=$arr[0];
			$img_info['h']=$arr[1];
			$this->assign('img', $img_info);
			$this->display('cutimg.html');
		}
		else 
		{
			die("images doesn't exist");
		}
	}
	
	public function act_cutimg()
	{
		$info['viewPortW']=$_POST['viewPortW'];
		$info['viewPortH']=$_POST['viewPortH'];
		$info['imageX']=$_POST['imageX'];
		$info['imageY']=$_POST['imageY'];
		$info['imageRotate']=$_POST['imageRotate'];
		$info['imageW']=$_POST['imageW'];
		$info['imageH']=$_POST['imageH'];
		$imageSource=$_POST['imageSource'];
		$info['selectorX']=$_POST['selectorX'];
		$info['selectorY']=$_POST['selectorY'];
		$info['selectorW']=$_POST['selectorW'];
		$info['selectorH']=$_POST['selectorH'];
		
		$result=array('error'=>0,'content'=>'裁剪成功','data'=>'');
		$res=Model('image')->crop_img($imageSource,$info);
		if($res)
		{
			$result['data']=$res;
		}
		else
		{
			$result['error']=1;
			$result['content']='裁剪失败';
		}
		print_r(json_encode($result));
	}
	
	public function uploadimg()
	{
		$cache_dir=UPLOAD_PATH.'/';
		$folder = @opendir($cache_dir);
		$dir=array();
		while ($file = readdir($folder))
		{
			if ($file == '.' || $file == '..' || $file == 'index.htm' || $file == 'index.html')
			{
				continue;
			}
			if (is_dir($cache_dir . $file))
			{
		
				$dir[]='/'.$file;
			}
		}
		$data=array();
		$this->assign('dir',$dir);
		$this->display('uploadimg.html');
	}
	
	public function act_upload()
	{
	    $path=$_POST['filepath'];
		$file=current($_FILES);
		$result=array('error'=>0,'url'=>$file['name']);
		$thumb_url=Model('image')->upload_image($file, $path.date('Ym'));
		$result['url']=$thumb_url;
		
		$result['message']='';
		print_r(json_encode($result));
		exit();
	}
	


	
	public function index()
	{
		$this->display('index.html');
	}
	
    public function clear_file()
	{
		$result=array('error'=>0,'content'=>'清除成功');
		$cache_dir=ROOT_PATH. '/Temp/Cache/';
		$folder = @opendir($cache_dir);
		
		if ($folder === false)
		{
			return false;
		}
		
		while ($file = readdir($folder))
		{
			if ($file == '.' || $file == '..' || $file == 'index.htm' || $file == 'index.html')
			{
				continue;
			}
			
			if (is_file($cache_dir . $file))
			{
				
				@unlink($cache_dir . $file);
			}
		}
		
		//清除smarty编译文件
		$cache_dir=ROOT_PATH. '/Temp/templates_c/';
		$folder = @opendir($cache_dir);
		
		if ($folder === false)
		{
			return false;
		}
		
		while ($file = readdir($folder))
		{
			if ($file == '.' || $file == '..' )
			{
				continue;
			}
			
			if (is_file($cache_dir . $file))
			{
		
				@unlink($cache_dir . $file);
			}
		}
		$this->showMsg($result);
	}
	
	public function login()
	{
		if($_COOKIE["QIAQIA"][admin_id]||$_COOKIE["QIAQIA"][admin_pass])
		{
			
		}
		$this->display('login.html');
	}

	/**
	 * 登录操作
	 */
	public function act_login()
	{
		$username = isset($_POST['username'])?hy_check($_POST['username'],'un',15):'';
		$password = isset($_POST['password'])?hy_check($_POST['password'],'',20):'';
		
		$captcha = isset($_POST['captcha'])?$_POST['captcha']:'';
		
		$check_vali = $this->validator($captcha);
		
		if(!$check_vali || $captcha == '')
		{
			$this->showMsg('验证码错误','index.php?g=admin&m=index&a=login');
		}
		if($username == '#@_error' || $password == '#@_error')
		{
			Error::log('[后台登录错误]'.$_POST['username'].'--'.hy_getIP(),ADMIN_LOG);
			$this->showMsg(Lang('账号或者密码错误'),'/qq_admin.php');
		}
		if(Model('index')->check_user($username, $password))
		{
			header("Location:index.php?g=admin&m=index&a=index");
		}
		else 
		{
			Error::log('[后台登录错误]'.$_POST['username'].'--'.hy_getIP(),ADMIN_LOG);
			$this->showMsg(Lang('账号或者密码错误'),'index.php?g=admin&m=index&a=login');
		}
	}
	
}

?>